import axios from 'axios'
import { ElMessage } from 'element-plus'
const service = axios.create({
  timeout: 5000
})

// 响应拦截
service.interceptors.response.use(
  (response) => {
    // console.log(response)
    // console.log(response)
    // console.log(data.code)
    if (response.status === 200) {
      return response
    } else {
      ElMessage.error(response.statusText)
      return Promise.reject(new Error(response.statusText))
    }
  },
  (error) => {
    console.log(error.response)
    error.response && ElMessage.error(error.response.data)
    return Promise.reject(new Error(error.response.data))
  }
)

// 请求拦截
service.interceptors.request.use(
  (config) => {
    config.headers.Authorization = localStorage.getItem('token')
    return config
  },
  (error) => {
    return Promise.reject(new Error(error))
  }
)
export default service
